package com.springboot.hiresystem.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.hiresystem.domain.Messages;
import com.springboot.hiresystem.dto.req.MessagesQueryRequestDTO;
import com.springboot.hiresystem.dto.req.MessagesUpdateRequestDTO;
import com.springboot.hiresystem.dto.resp.MessagesResponseDTO;
import com.springboot.hiresystem.dto.resp.PageResponse;
import com.springboot.hiresystem.dto.resp.Result;
import com.springboot.hiresystem.service.MessagesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/messages")
@Slf4j
public class MessagesController {

    private final MessagesService messagesService;

    public MessagesController(MessagesService messagesService) {
        this.messagesService = messagesService;
    }

    // 分页查询
    @GetMapping("/list")
    public Result<PageResponse<MessagesResponseDTO>> list(MessagesQueryRequestDTO request) {
        QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();
        if (request.getMessageId() != null) {
            queryWrapper.eq("message_id", request.getMessageId());
        }
        queryWrapper.orderByAsc("message_id");

        Page<Messages> page = new Page<>(request.getPage(), request.getSize());
        Page<Messages> resultPage = messagesService.page(page, queryWrapper);

        List<MessagesResponseDTO> dtoList = BeanUtil.copyToList(resultPage.getRecords(), MessagesResponseDTO.class);

        PageResponse<MessagesResponseDTO> response = new PageResponse<>();
        response.setTotal(resultPage.getTotal());
        response.setList(dtoList);

        return Result.success(response);
    }

    // 条件查询
    @PostMapping("/query")
    public Result<List<MessagesResponseDTO>> query(@RequestBody MessagesQueryRequestDTO request) {
        QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();
        //request中发送过来两个id,请查询两方分别作为sender和receiver的情况的查询结果,需要满足id1为发送方的同时,id2为接收方,或id1为接收方时id2为发送方
        if (request.getSenderId() != null && request.getReceiverId() != null) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("sender_id", request.getSenderId()).eq("receiver_id", request.getReceiverId());
            });
            queryWrapper.or(wrapper -> {
                wrapper.eq("sender_id", request.getReceiverId()).eq("receiver_id", request.getSenderId());
            });
        }
        queryWrapper.orderByAsc("sent_at");
        List<Messages> messages = messagesService.list(queryWrapper);
        List<MessagesResponseDTO> dtoList = BeanUtil.copyToList(messages, MessagesResponseDTO.class);

        return Result.success(dtoList);
    }

    // 更新或添加
    @PostMapping("/update")
    public Result<String> update(@RequestBody MessagesUpdateRequestDTO request) {
        Messages message = BeanUtil.copyProperties(request, Messages.class);
        if (message.getMessageId() != null && message.getMessageId() > 0) {
            messagesService.updateById(message);
            return Result.success("更新成功");
        } else {
            if (!messagesService.save(message)) {
                return Result.failure(500, "添加失败");
            }
            return Result.success("新增成功");
        }
    }

    // 删除
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody Messages message) {
        messagesService.removeById(message.getMessageId());
        return Result.success("删除成功");
    }
    //根据单人id获取
    @PostMapping("/getByUserId")
    public Result<List<MessagesResponseDTO>> getByUserId(@RequestBody MessagesQueryRequestDTO request) {
        QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();
        //request中发送一个id,当id为发送方或者接收方时,返回该用户所有消息
        if (request.getSenderId() != null) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("sender_id", request.getSenderId()).or().eq("receiver_id", request.getSenderId());
            });
        }
        if (request.getReceiverId() != null) {
            queryWrapper.and(wrapper -> {
                wrapper.eq("sender_id", request.getReceiverId()).or().eq("receiver_id", request.getReceiverId());
            });
        }
        queryWrapper.orderByAsc("sent_at");
        List<Messages> messages = messagesService.list(queryWrapper);
        List<MessagesResponseDTO> dtoList = BeanUtil.copyToList(messages, MessagesResponseDTO.class);
        return Result.success(dtoList);
    }

}